package com.zcgame.base.utils

import java.util.*

/**
 *@author:huangLJ
 *@date:2021/5/27 18:45
 */
/**
 * 随机生成字符串+字母的工具
 */
object StringRandom {
    fun getStringRandom(length:Int):String{
        var s = ""
        val random = Random()

        //参数length，表示生成几位随机数
        for (i in 0 until length) {
            val charOrNum = if (random.nextInt(2) % 2 == 0) "char" else "num"
            //输出字母还是数字
            if ("char".equals(charOrNum, ignoreCase = true)) {
                //输出是大写字母还是小写字母
                val temp = if (random.nextInt(2) % 2 == 0) 65 else 97
                s += (random.nextInt(26) + temp).toChar()
            } else if ("num".equals(charOrNum, ignoreCase = true)) {
                s += random.nextInt(10).toString()
            }
        }
        return s
    }
}

fun main() {
    val stringRandom = StringRandom
    print(stringRandom.getStringRandom(8))
}